草庐IT

C++11 auto 和 size_type

全部标签

c++ - 使用 std::map 的 Visual Studio 11 编译时错误

以下代码可以使用gcc-4.5.1编译,但不能在VisualStudio11中编译。#include#includetypedefstd::pair>pairus;intmain(){std::map>x;std::arraytroll={1,2,3,4};x.insert(pairus(1,troll));autoz=x[1];}1现在映射到std::arraytroll.插入效果很好,程序可以编译。但是,一旦我尝试autoz=x[1]->因此试图获得1的数组troll被映射到,程序不编译并出现以下错误:errorC2512:'std::array::array':没有合适的默认构造

c++ - std::is_convertible 用于 type_info

在C++11中,可以通过usingstd::is_convertible确定类型A的变量是否可以隐式转换为类型B.如果你真的知道类型A和B,这很有效,但我只有type_infos。所以我正在寻找的是这样的功能:boolmyIsConvertible(consttype_info&from,consttype_info&to);是否可以在C++中实现类似的东西?如果是,怎么办? 最佳答案 在可移植的C++中做你想做的事是不可能的。可能如果您将自己限制在给定的平台上,则有可能获得部分答案。例如那些遵守ItaniumABI的平台将实现此功

c++ - C++11 中是否有在模板中获取成员指针类型的方法?

我知道这在C++03中是不可能的,但我希望有一些新的巫术允许我这样做。见下文:templatestructBinder{templatevoidAddMatch();};structTestType{inti;};intmain(intargc,char**argv){Binderb;b.AddMatch();//Ihavetodothisnowb.AddMatch();//I'dliketobeabletodothis(i.e.inferfieldtype)}在C++11中有什么方法可以做到这一点吗?decltype有帮助吗?**更新:使用Vlad的示例,我在想这样的事情会起作用(警

C++11 元编程 - 在编译期间查找枚举值(值包含间隙)

有没有办法在编译时验证给定的值在给定枚举的值范围内,因此有效?enummyenum{val1=10,val2=30,val3=45}templateclassmyClass{...}myClassa;//fails,OKmyClassb;//compilesOKmyClassc;//compiles,NOTOK!在那里使用第二个模板非类型bool参数会很有用,该bool值的值将由元函数给出,给定的值将验证该值是否在myenum的值内。我查看了各种与枚举相关的问题,例如如何迭代枚举,但似乎无法完成。 最佳答案 enummyenum{v

c++ - CRTP + 特征类 : "no type named..."

我尝试使用模板化类实现CRTP,但在使用以下示例代码时出现错误:#includetemplateclassTraits{public:typedeftypenameT::typetype;//'staticconstunsignedintm_const=T::m_const;staticconstunsignedintn_const=T::n_const;staticconstunsignedintsize_const=T::m_const*T::n_const;};templateclassCrtp{public:typedeftypenameTraits::typecrtp_typ

C++11:Variadic模板函数参数包扩展执行顺序

考虑以下代码:templatesize_tf(Tt,size_t&x){returnx++;}templatevoidg(Args...args){size_tx=0;size_ty[]={f(args,x)...};for(size_ti=0;iC++11标准保证断言不会触发吗?为什么或为什么不? 最佳答案 是的,它保证不会开火。请参阅以下引述:§14.5.3可变模板:Packexpansionscanoccurin[...]aninitializer-list;thepatternisaninitializer-clause.§

c++ - 为什么在使用嵌套的 OpenMP pragma 时 c++11 线程变得不可连接?

以下代码应该非常简单,但在尝试使用嵌套的OpenMP代码在线程上执行.join()时,似乎最终陷入了挂起状态。使用GCC编译器4.7.2x64和来自http://sourceforge.net/projects/mingwbuilds的pthreads使用g++threadexample.cpp-Wall-std=c++11-fopenmp-othreads//threadexample.cpp#include#include#includeusingnamespacestd;voidhello(inta){#pragmaompparallelforfor(inti=0;i

c++ - std::string::max_size() 作为静态成员

为什么max_size不是std::string的静态成员?这可以编译,但我觉得奇怪的是所有字符串共有的属性只能通过字符串的实例访问:std::size_tmax_size=std::string().max_size();为什么会这样实现? 最佳答案 Whyisn'tmax_sizeastaticmemberofstd::string?因为max_size返回值取决于字符串实例内部使用的分配器实例。 关于c++-std::string::max_size()作为静态成员,我们在Stac

c++ - 如何在 Visual Studio 2010 速成版上启用 C++11 编译器?

我正在使用使用lambda的tbb::parallel_for函数。我收到以下代码的语法错误:voidparallel_relax(Classobject,std::vectorverList){tbb::parallel_for(blocked_range(0,verList.size()),[=](constblocked_range&r){for(Vertex*vit=r.begin();vit!=r.end();++vit){Vertex*v=vit;object.function(v);}});}语法错误:syntaxerror:'['1>main.cpp(16):error

c++ - std::string 的 type_traits 段错误

从UsingSFINAEtocheckforglobaloperator收集信息和templates,decltypeandnon-classtypes,我得到了以下代码:http://ideone.com/sEQc87基本上,我将两个问题的代码结合起来,如果它有ostream声明,则调用print函数,否则调用to_string方法。摘自问题1namespacehas_insertion_operator_impl{typedefcharno;typedefcharyes[2];structany_t{templateany_t(Tconst&);};nooperatorstruct